package net.siufung.market.base.region.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.siufung.market.base.region.api.vo.RegionApiDto;
import net.siufung.market.base.region.controller.assembler.RegionAssembler;
import net.siufung.market.base.region.enums.RegionLevels;
import net.siufung.market.base.region.mapper.IRegionMapper;
import net.siufung.market.base.region.mapper.entity.Region;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author 陈建峰
 * @since 2022/7/7 2:42 下午
 */
@Slf4j
@AllArgsConstructor
@Component("regionApiImpl")
public class RegionApiImpl implements IRegionApi {

    private final IRegionMapper regionMapper;

    @Override
    public List<RegionApiDto> getProvinceList() {
        return RegionAssembler.fromList(
                    regionMapper.selectList(
                        new LambdaQueryWrapper<Region>()
                                .eq(Region::getLevel, RegionLevels.PROVINCES.getValue())));
    }

    @Override
    public List<RegionApiDto> getCityList() {
        return RegionAssembler.fromList(
                regionMapper.selectList(
                        new LambdaQueryWrapper<Region>()
                                .eq(Region::getLevel, RegionLevels.CITIES.getValue())));
    }

    @Override
    public List<RegionApiDto> getCityList(Integer provinceId) {
        return RegionAssembler.fromList(
                regionMapper.selectList(
                        new LambdaQueryWrapper<Region>()
                                .eq(Region::getLevel, RegionLevels.CITIES.getValue())
                                .eq(Region::getParentId, provinceId)));
    }

    @Override
    public List<RegionApiDto> getAreaList() {
        return RegionAssembler.fromList(
                regionMapper.selectList(
                        new LambdaQueryWrapper<Region>()
                                .eq(Region::getLevel, RegionLevels.AREAS.getValue())));
    }

    @Override
    public List<RegionApiDto> getAreaList(Integer cityId) {
        return RegionAssembler.fromList(
                regionMapper.selectList(
                        new LambdaQueryWrapper<Region>()
                                .eq(Region::getLevel, RegionLevels.AREAS.getValue())
                                .eq(Region::getParentId, cityId)));
    }
}
